<div class="row-fluid">
	<div class="span12">
		<ul class="breadcrumb">
			<li>
				<a href="index.php">
					<i class="radmin-icon radmin-home"></i>
					<?php echo t('page', 'Dashboard'); ?>
				</a>
				<span class="divider">/</span>
			</li>
			<li>
				<a href="backups.php">
					<i class="radmin-icon radmin-cabinet"></i>
					<?php echo t('page', 'Backups'); ?>
				</a>
				<span class="divider">/</span>
			</li>
			<li class="active">
				<i class="radmin-icon radmin-drawer-2"></i>
				<?php echo t('page', 'Backup Schedule'); ?>
			</li>
		</ul>
	</div>
</div>

<div class="row-fluid">
	<div class="span9">
		<h4 class="title"><?php $this->widget('application.components.mywidgets.WikiHelp', array('wiki' => 'backup_schedule')); ?> Backup Schedule</h4>
		<div class="squiggly-border"></div>
		<div class="row-fluid">
			<div class="span12 hide" id="backupschedule_error_info">
				<div class="alert alter-block alert-error">
		<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
					<h4>Error</h4>
					<div class="content"></div>
				</div>
			</div>
		</div>
		
		<?php $this->widget('zii.widgets.grid.CGridView', array(
			'id' => 'backupschedule_grid',
			'summaryText' => '',
			'dataProvider' => $backupSchedule->search(),
			'columns' => array(
				array(
					'header' => 'Id',
					'value' => '$data->id_backup_schedule',
					'headerHtmlOptions' => array('class' => 'hide'),
					'htmlOptions' => array('class' => 'hide id_backup_schedule')
				),
				array(
					'header' => t('page', 'Name'),
                    'value' => '$data->name',
					'htmlOptions' => array('class' => 'name'),
					'headerHtmlOptions' => array('class' => 'name_sort'),
					'name' => 'username_sort',
					'sortable' => true
				),
				array(
					'header' => t('page', 'Location'),
                    'value' => 'empty($data->BackupLocation)? "" : $data->BackupLocation->name',
					'htmlOptions' => array('class' => 'id_backup_location'),
					'headerHtmlOptions' => array('class' => 'schedule_sort'),
					'name' => 'schedule_sort',
					'sortable' => true
				),
				array(
					'header' => t('page', 'Status'),
					'value' => '$data->statusTxt',
					'htmlOptions' => array('class' => 'status'),
					'headerHtmlOptions' => array('class' => 'status_sort'),
					'name' => 'status_sort',
					'sortable' => true
				),
				array(
					'header' => t('page', 'Days'),
					'value' => '$data->daysTxt',
					'headerHtmlOptions' => array('class' => 'days_sort'),
					'htmlOptions' => array('class' => 'days'),
					'name' => 'days_sort',
					'sortable' => true
				),
				array(
					'header' => t('page', 'Hour'),
					'type' => 'raw',
					'value' => '$data->hour',
					'headerHtmlOptions' => array('class' => 'hour_sort'),
					'htmlOptions' => array('class' => 'hour'),
					'name' => 'hour_sort',
					'sortable' => true
				),
				array(
					'header' => t('page', 'Action'),
					'type' => 'raw',
					'value' => '$data->renderListActionButtons()'
				)
			),
			'itemsCssClass' => 'table table-index table-striped',
			'pagerCssClass' => 'pagination pagination-right',
			'pager' => array(
				'class' => 'LinkPager',
			),
			'htmlOptions' => array(
				'class' => ''
			)
		));?>
		
		<script>
			$(document).ready(function(){
//				$('#backupschedule_grid .pagination').before($('.pull-left'));
//				$('.pull-left').after('<br /><br />');
				
				var sort = <?php echo json_encode($sort); ?>;
				sort = sort.split('.');
				if(sort[1] == 'desc'){
					var arrow = $('<i class="icon-arrow-down hidden-phone"></i>');
				}else{
					var arrow = $('<i class="icon-arrow-up hidden-phone"></i>');
				}
				$('#backupschedule_grid th.'+sort[0]).append(arrow);				
			});
		</script>
	</div>
	
	
	<?php $this->renderPartial('forms/_add', array('backupScheduleAddForm' => $backupScheduleAddForm)); ?>
	<?php $this->renderPartial('forms/_edit', array('backupScheduleEditForm' => $backupScheduleEditForm)); ?>

	<script>
		function ReplaceFirstUper(str)  
		{     
		    str = str.toLowerCase();     
		    return str.replace(/\b(\w)|\s(\w)/g, function(m){  
		        return m.toUpperCase();  
		    });    
		} 
	
		$('#backupschedule_grid .backupschedule_edit').live('click', function(){
			var id_backup_schedule = $(this).parents('tr').find('.id_backup_schedule').text();
			$('.backupschedule_edit-line').removeClass('backupschedule_edit-line');
			$(this).parents('tr').addClass('backupschedule_edit-line');
			$.ajax({
				url: <?php echo json_encode(url('backup/schedule/detail')); ?>,
				type: 'get',
				data: {id_backup_schedule: id_backup_schedule},
				success: function(detail){
					$('#backupschedule-edit-form .fields').val('');
					$('#BackupScheduleEditForm_days option').removeAttr('selected').trigger('liszt:updated');
					$.each(detail, function(key, value){
						if($('#backupschedule-edit-form #BackupScheduleEditForm_'+key)){
							if(key == 'cpanel_accounts'){
								var id_cpanel_accounts = value.split(',');
								$('#BackupScheduleEditForm_cpanel_accounts').html('');
								$.each(id_cpanel_accounts, function(k, id_cpanel_account){
									$.ajax({
										type: 'get',
										url: '<?php echo url('whm/cpanelaccount/detail'); ?>',
										data: {
											id_cpanel_accounts: id_cpanel_account
										},
										success: function(ret){
											$('#BackupScheduleEditForm_cpanel_accounts').append(
												$('<option />').attr({
													'value': ret.id_cpanel_accounts,
													'selected': 'selected'
												}).text(ret.domain)
											);
											$('#BackupScheduleEditForm_cpanel_accounts').change().trigger('liszt:updated');
										}, 
										dataType: 'json'
									});
								});
							}else if(key == 'sun' || key == 'mon' || key == 'tue' || key == 'wed' || key == 'thur' || key == 'fri' || key == 'sat'){
								if(value == 1){
									$('#BackupScheduleEditForm_days option[value="'+ReplaceFirstUper(key)+'"]').attr('selected', 'selected');
									$('#BackupScheduleEditForm_days option').trigger('liszt:updated');
								}
							}else{
								$('#backupschedule-edit-form #BackupScheduleEditForm_'+key).val(value);
							}
						}
					});
					$('#BackupScheduleEditForm_protocol').change();
					
					$('#backupschedule-add-block').fadeOut(function(){
						$('#backupschedule-edit-block').fadeIn();
					});
				},
				dataType: 'json'
			});
		});
		
		$('#backupschedule_grid .backupschedule_delete').live('click', function(){
			$(this).parents('.btn-group').find('.actions').hide();
			$(this).parents('.btn-group').find('.delete_confirm_btn').show();
		});
		$('#backupschedule_grid .delete_cancel').live('click', function(){
			$(this).parents('.btn-group').find('.actions').show();
			$(this).parents('.btn-group').find('.delete_confirm_btn').hide();
		});
		$('#backupschedule_grid .delete_confirm').live('click', function(){
			var obj = $(this);
			$(this).parents('.btn-group').find('button.actions').attr('data-loading-text', 'deleting').button('loading');
			var id_backup_schedule = $(this).parents('tr').find('.id_backup_schedule').text();
			$.ajax({
				url: '<?php echo url('backup/schedule/delete'); ?>',
				type: 'post',
				data: {id_backup_schedule: id_backup_schedule},
				success: function(ret){
					obj.parents('.btn-group').find('button.actions').button('reset');
					if(ret.result){
						obj.parents('tr').remove();
					}else{
						$("#backupschedule_error_info").blink({
							"beforeCall": function(){
								$("#backupschedule_error_info .content").html("");
								$.each(ret["errors"], function(attrName, error){
									$(error).each(function(k, e){
										var p = $("<p />").addClass("text-error").append(attrName+": "+e);
										$("#backupschedule_error_info .content").append(p);
									});
								});
							}
						});
					}
					obj.parents('.btn-group').find('.delete_cancel').click();
				},
				dataType: 'json'
			});
		});

		$('#switch-backupschedule-add-btn').click(function(){
			$('#backupschedule-edit-block').fadeOut(function(){
				$('#backupschedule-add-block').fadeIn();
			});
		});
	</script>
</div>